<template>
  <div class="bgc">
    <!-- 导航栏 -->
    <van-nav-bar title="Sign In" fixed placeholder />
    <!-- 图片 -->
    <div class="logo"></div>
    <!-- 表单组件 -->
    <van-form @submit="doLogin">
      <div class="take">
        <van-field
          colon
          v-model="form.mobile"
          label="手机号"
          placeholder="请输入手机号"
          :rules="rules.mobile"
          label-width:100px
        />
        <van-field
          colon
          v-model="form.code"
          label="验证码"
          placeholder="请输入验证码"
          :rules="rules.code"
        />
      </div>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLoading"
          loading-text="登录中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api/user'
export default {
  data() {
    return {
      form: {
        mobile: '13122223333',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号码格式' }
        ],
        code: [
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '请输入6位数验证码' }
        ]
      },
      isLoading: false
    }
  },
  methods: {
    async doLogin() {
      this.isLoading = true
      try {
        const res = await loginAPI(this.form)
        this.$store.commit('setToken', res.data)
        if (this.$route.query.back) {
          this.$router.push(this.$route.query.back)
        } else {
          this.$router.push('/layout/home')
        }
        this.$toast.success('登录成功')
      } catch {
        this.$toast.fail('验证码错误')
      }
      this.isLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
.bgc {
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.4);
  display: block;
  position: relative;
}
.bgc::before {
  content: '';
  width: 100vw;
  height: 100vh;
  background: url(@/assets/bgc3.jpg) no-repeat center / cover;
  opacity: 0.8;
  top: 0px;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;
}
.logo {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  margin-top: 30px;
  background: url(@/assets/logo.png) no-repeat center / cover;
}
.van-form .take {
  width: 310px;
  height: 100%;
  border: 1px solid #fff;
  border-radius: 16px;
  margin-left: 32px;
  margin-right: 32px;
  margin-top: 80px;
  margin-bottom: 50px;
}
.van-form .take .van-cell {
  background-color: transparent;
  padding: 20px;
}
::v-deep .van-form .take .van-cell .van-cell__title {
  color: aliceblue;
}
::v-deep .van-form .take .van-cell .van-field__label {
  margin-right: -20px;
}
::v-deep .van-form .take .van-cell .van-cell__value .van-field__control {
  color: #fff;
}
.van-button.van-button--info {
  background-color: #d0355d;
  border: 1px solid #d0355d;
}
.van-button.van-button--round {
  width: 300px;
  margin: 0 auto;
}
::v-deep [class*='van-hairline']::after {
  border: none !important;
}
::v-deep .van-nav-bar {
  background-color: transparent !important;
  padding-top: 20px;
}
::v-deep .van-nav-bar .van-nav-bar__title {
  font-size: 20px;
  color: #fff;
}
</style>
